package com.cqeec.gams.service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cqeec.gams.common.Function;
import com.cqeec.gams.dao.FunctionalModuleMapper;
import com.cqeec.gams.dao.RolePermissionsMapper;
import com.cqeec.gams.entity.FunctionalModule;
import com.cqeec.gams.entity.RolePermissions;

/**
 * 功能模块服务实现。
 * 
 * @author 唐礼飞
 *
 */
@Service
public class FunctionalModuleServiceImpl implements FunctionalModuleService {
	@Autowired
	private FunctionalModuleMapper functionalModuleMapper;
	@Autowired
	private RolePermissionsMapper rpMapper;

	@Override
	public long count() {
		return functionalModuleMapper.count();
	}

	@Override
	public List<FunctionalModule> findAll() {
		return functionalModuleMapper.findAll();
	}

	@Override
	public FunctionalModule findById(String id) {
		return functionalModuleMapper.findById(id);
	}

	@Override
	public void insert(FunctionalModule entity) {
		if (entity.getId() == null || "".equals(entity.getId())) {
			entity.setId(UUID.randomUUID().toString());
		}
		functionalModuleMapper.insert(entity);
	}

	@Override
	public void update(FunctionalModule entity) {
		functionalModuleMapper.update(entity);
	}

	@Override
	public void delete(String id) {
		functionalModuleMapper.delete(id);
	}

	@Override
	public List<FunctionalModule> getAllFunctionGroup() {
		return functionalModuleMapper.findAllFunctionGroup();
	}

	@Override
	public int countRolePermission(String id) {
		return rpMapper.countRolePermission(id);
	}

	@Override
	public int countSubFunctionalModule(String id) {
		return functionalModuleMapper.countSubFunctionalModule(id);
	}

	@Override
	public List<Function> getFunction() {
		List<Function> list = new ArrayList<Function>();
		// 查找所有功能
		List<FunctionalModule> functionModuleList = functionalModuleMapper.findAll();
		// 用于临时存储功能组，方便后移除
		List<FunctionalModule> tempList = new ArrayList<FunctionalModule>();
		// 封装功能组
		Function function;
		for (FunctionalModule fm : functionModuleList) {
			if ("1".equals(fm.getType())) {
				function = new Function();
				function.setFunctionGroup(fm);
				list.add(function);
				tempList.add(fm);
			}
		}
		// 移除功能组
		functionModuleList.removeAll(tempList);
		tempList.clear();
		// 封装功能组的功能模块
		String functionGroupId;
		for (Function f : list) {
			functionGroupId = f.getFunctionGroup().getId();
			for (FunctionalModule fm : functionModuleList) {
				if (functionGroupId.equals(fm.getParent())) {
					f.addFunctionModule(fm);
					tempList.add(fm);
				}
			}
			functionModuleList.removeAll(tempList);
			tempList.clear();
		}
		return list;
	}

	@Override
	public List<Function> getFunction(String roleId) {
		List<Function> list = this.getFunction();
		// 将角色拥有的功能模块查询出来
		List<RolePermissions> rolePermissions = rpMapper.findByRole(roleId);
		// 循环设置模块是否选中
		String fmId;
		FunctionalModule fm;
		List<FunctionalModule> fmList;
		for (RolePermissions rp : rolePermissions) {
			fmId = rp.getFunctionalModule();
			for (Function f : list) {
				// 检测功能组是否被授权
				fm = f.getFunctionGroup();
				if (fmId.equals(fm.getId())) {
					fm.setChecked(true);
				}
				// 检测功能组的功能模块是否被授权
				fmList = f.getFunctionalModules();
				for (FunctionalModule fmd : fmList) {
					if (fmId.equals(fmd.getId())) {
						fmd.setChecked(true);
					}
				}
			}
		}
		return list;
	}

}
